package orgd

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "org"
)

// DomainOption 域构建参数
type DomainOption struct {
	OrgRepo OrgRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) OrgDomain {
	d := &orgDomain{}
	d.Init(d, p, DomainID)
	initOrg(d, opt.OrgRepo)
	return d
}

// OrgDomain 信息域
type OrgDomain interface {
	ddd.Domain
	OrgAggregateRoot() OrgAggregateRoot
}
type orgDomain struct {
	ddd.BaseDomain
	orgAggregateRoot OrgAggregateRoot
}

func (i *orgDomain) OrgAggregateRoot() OrgAggregateRoot {
	if i.orgAggregateRoot != nil {
		return i.orgAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(DomainID)
	if r == nil {
		panic("没有初始化OrgAggregateRoot")
	}
	ir, ok := r.(OrgAggregateRoot)
	if !ok {
		panic("初始化OrgAggregateRoot，类型错误")
	}
	i.orgAggregateRoot = ir
	return ir

}
